package com.example.mypra1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 性格测试
 */
public class PersonalityActivity extends AppCompatActivity {
    private ViewFlipper viewFlipper;//切换题目控件
    private Button last,next,xinge_submit;
    private TextView question1,question2,question3,question4;
    private RadioButton q1a1,q1a2,q2a1,q2a2,q3a1,q3a2,q4a1,q4a2;//问题选项
    private String q11,q12,q21,q22,q31,q32,q41,q42,answer;//问题及我的答案
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            Map<String,Object> map = (Map<String, Object>) msg.obj;
             question1.setText(map.get("q1").toString());
             question2.setText(map.get("q2").toString());
             question3.setText(map.get("q3").toString());
             question4.setText(map.get("q4").toString());
             q1a1.setText(map.get("q1a1").toString());
            q1a2.setText(map.get("q1a2").toString());
            q2a1.setText(map.get("q2a1").toString());
            q2a2.setText(map.get("q2a2").toString());
            q3a1.setText(map.get("q3a1").toString());
            q3a2.setText(map.get("q3a2").toString());
            q4a1.setText(map.get("q4a1").toString());
            q4a2.setText(map.get("q4a2").toString());
            q11 = map.get("q11").toString();
            q12 = map.get("q12").toString();
            q21 = map.get("q21").toString();
            q22 = map.get("q22").toString();
            q31 = map.get("q31").toString();
            q32 = map.get("q32").toString();
            q41 = map.get("q41").toString();
            q42 = map.get("q42").toString();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personality);
        xinge_submit = findViewById(R.id.xinge_submit);
        viewFlipper = findViewById(R.id.flipper);
        question1 = findViewById(R.id.question1);
        question2 = findViewById(R.id.question2);
        question3 = findViewById(R.id.question3);
        question4 = findViewById(R.id.question4);
        q1a1 = findViewById(R.id.q1a1);
        q1a2 = findViewById(R.id.q1a2);
        q2a1 = findViewById(R.id.q2a1);
        q2a2 = findViewById(R.id.q2a2);
        q3a1 = findViewById(R.id.q3a1);
        q3a2 = findViewById(R.id.q3a2);
        q4a1 = findViewById(R.id.q4a1);
        q4a2 = findViewById(R.id.q4a2);
        last = findViewById(R.id.last);
        next = findViewById(R.id.next);
        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LinearLayout linearLayout =(LinearLayout) viewFlipper.getCurrentView();
                if(linearLayout.getId()==R.id.q1){
                    Toast.makeText(getApplicationContext(),"没有上一题",Toast.LENGTH_LONG).show();
                }
                else
                     viewFlipper.showPrevious();
            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               LinearLayout linearLayout =(LinearLayout) viewFlipper.getCurrentView();
               if(linearLayout.getId()==R.id.q4){
                   Toast.makeText(getApplicationContext(),"已是最后一题",Toast.LENGTH_LONG).show();
               }
               else
                    viewFlipper.showNext();
            }
        });
        getData();
        xinge_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                answer="";
                if(q1a1.isChecked())
                    answer+=q11;
                else
                    answer+=q12;
                if(q2a1.isChecked())
                    answer+=","+q21;
                else
                    answer+=","+q22;
                if(q3a1.isChecked())
                    answer+=","+q31;
                else
                    answer+=","+q32;
                if(q4a1.isChecked())
                    answer+=","+q41;
                else
                    answer+=","+q42;
                Intent intent = new Intent(PersonalityActivity.this,SuccessActivity.class);
                intent.putExtra("answer",answer);
                startActivity(intent);
                finish();

            }
        });
    }
    //发送get请求，获取四道题目
    private void getData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://apis.juhe.cn/fapig/character_test/questions?key=f9c99bfe72cf73e7c03d317bcd3c191a")
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException,RuntimeException {
                            Log.d("resp",response.toString());
                            if (response.isSuccessful()) {
                                String res = response.body().string();
                                Log.d("res",res);
                                try {
                                    JSONObject jsonObject1 = new JSONObject(res);
                                    JSONArray jsonArray = jsonObject1.getJSONArray("result");
                                    Map<String,String>map = new HashMap<>();
                                    Message message = new Message();
                                    message.obj = map;
                                    map.put("q1","1、"+jsonArray.getJSONObject(0).getString("q"));
                                    map.put("q1a1","A."+jsonArray.getJSONObject(0).getString("a"));
                                    map.put("q1a2","B."+jsonArray.getJSONObject(0).getString("b"));
                                    map.put("q11",jsonArray.getJSONObject(0).getString("ia"));
                                    map.put("q12",jsonArray.getJSONObject(0).getString("ib"));
                                    map.put("q2","2、"+jsonArray.getJSONObject(1).getString("q"));
                                    map.put("q2a1","A."+jsonArray.getJSONObject(1).getString("a"));
                                    map.put("q2a2","B."+jsonArray.getJSONObject(1).getString("b"));
                                    map.put("q21",jsonArray.getJSONObject(1).getString("ia"));
                                    map.put("q22",jsonArray.getJSONObject(1).getString("ib"));
                                    map.put("q3","3、"+jsonArray.getJSONObject(2).getString("q"));
                                    map.put("q3a1","A."+jsonArray.getJSONObject(2).getString("a"));
                                    map.put("q3a2","B."+jsonArray.getJSONObject(2).getString("b"));
                                    map.put("q31",jsonArray.getJSONObject(2).getString("ia"));
                                    map.put("q32",jsonArray.getJSONObject(2).getString("ib"));
                                    map.put("q4","4、"+jsonArray.getJSONObject(3).getString("q"));
                                    map.put("q4a1","A."+jsonArray.getJSONObject(3).getString("a"));
                                    map.put("q4a2","B."+jsonArray.getJSONObject(3).getString("b"));
                                    map.put("q41",jsonArray.getJSONObject(3).getString("ia"));
                                    map.put("q42",jsonArray.getJSONObject(3).getString("ib"));
                                    handler.sendMessage(message);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }else{
                                Log.d("resp",response.toString());
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}